onload = function() {



  var activityList =[];
  var pageNum=1;

  $('#categorypageBack').tap(function(){
    window.history.back();
  });

  var activityScroll = new IScroll('#activityScrollWrapper', {
    probeType: 3,
    mouseWheel: true,
  })

  activityScroll.scrollBy(0, -40);

  var head = $('.activityListHeader img');
  var foot = $('.activityListFooter img');
  activityScroll.on('scroll', function() {
     $('#activityScrollWrapper').trigger('scroll');
    var y=this.y,
        maxY=this.maxScrollY-y;
        if (y>=0) {
          // head = $('.activityListHeader img');
           head.addClass('up');
           return;
        }

        if (maxY >= 0) {
          // var foot = $('.activityListFooter img');
          foot.addClass('down')
          return;
        }
  })

  activityScroll.on('scrollEnd', function(){

    // 下拉刷新
    if (this.y >= -40 && this.y <0) {
      activityScroll.scrollTo(0,-40);
      head.removeClass('up')
    } else if (this.y >= 0) {
      head.attr('src', '../images/ajax-loader.gif');
      pageNum = 1;
      $.ajax({
        url: '/api/m_v1/dynamic',
        data: {
          pageNum: pageNum
        },
        contentType : "application/json; charset=utf-8",
        dataType:'json',
        success: function(res) {
          activityScroll.scrollTo(0, -40);
          head.removeClass('up');
          head.attr('src', '../images/arrow.png');
          activityList=res.goodsCate.list;
          var list = template('activityList', res.goodsCate)
          $('#activityListWrapper').html(list)
          
          activityScroll.refresh();

          //图片懒加载
          var myLazyLoad = new LazyLoad();

        }
      })
    }

    // 上拉加载更多
    var maxY = this.maxScrollY - this.y;
    if (maxY > -40 && maxY < 0) {
      activityScroll.scrollTo(0,this.maxScrollY + 40);
      foot.removeClass('dpwn');
    } else if (maxY >=0) {
      foot.attr('src', '../images/ajax-loader.gif');
      pageNum ++;
      console.log(pageNum);
      $.ajax({
        url: '/api/m_v1/dynamic',
        data: {
          pageNum: pageNum
        },
        contentType : "application/json; charset=utf-8",
        dataType:'json',
        success: function(res) {
          // activityScroll.scrollTo(0, self.y + 40);
          foot.removeClass('up');
          foot.attr('src', '../images/arrow.png');
          activityList=activityList.concat(res.goodsCate.list)
          var list = template('activityList', {'list':activityList})
          $('#activityListWrapper').html(list)

          activityScroll.refresh();

          var myLazyLoad = new LazyLoad();

        }
      })
    }
  })

  $.ajax({
    url: '/api/m_v1/dynamic',
    data: {
      pageNum: 2
    },
    contentType : "application/json; charset=utf-8",
    dataType:'json',
    success: function(res) {
      var list = template('activityList', res.goodsCate)
      $('#activityListWrapper').html(list)

      activityScroll.refresh();

      var myLazyLoad = new LazyLoad();

    }
  })


}
